package cn.mazexal.exambg.controller;

import cn.mazexal.exambg.common.UserTokenManager;
import cn.mazexal.exambg.common.anno.PermitGuest;
import cn.mazexal.exambg.common.models.SingleResponse;
import cn.mazexal.exambg.models.db.AdminUserDO;
import cn.mazexal.exambg.models.form.LoginForm;
import cn.mazexal.exambg.services.AdminUserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理
 *
 * @author: Mazexal
 * Date: 2025/5/13
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private AdminUserService adminUserService;

    /**
     * 用户登录
     *
     * @param loginModel
     * @return
     */
    @RequestMapping("/login")
    @PermitGuest
    public SingleResponse login(@RequestBody LoginForm loginModel) {
        Long userId = adminUserService.userLogin(loginModel.getUserName(), loginModel.getPassWord());
        if (userId == 0) {
            return SingleResponse.error("500","用户名或密码错误");
        }
        return SingleResponse.of(UserTokenManager.generateToken(userId));
    }

}
